package com.owbtgad.common.base

import android.view.LayoutInflater
import androidx.databinding.ViewDataBinding
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.ParameterizedType

/**
 * 创建日期 2020/9/24
 * 描述：
 */
abstract class BaseDActivity<VDB : ViewDataBinding> : BaseActivity() {
    protected lateinit var mBinding: VDB

    override fun initContentView() {
        val type = javaClass.genericSuperclass as ParameterizedType
        val cls = type.actualTypeArguments[0] as Class<*>
        try {
            val inflate = cls.getDeclaredMethod("inflate", LayoutInflater::class.java)
            mBinding = inflate.invoke(null, layoutInflater) as VDB
            mBinding!!.lifecycleOwner = this
            setContentView(mBinding!!.root)
        } catch (e: NoSuchMethodException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        } catch (e: InvocationTargetException) {
            e.printStackTrace()
        }
    }

    override fun getContentLayout(): Int {
        return 0
    }

    override fun onDestroy() {
        super.onDestroy()
        if (mBinding != null) {
            mBinding!!.unbind()
        }
    }
}